package com.a3dweijing.wetouch2.Activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.GlideUtil;
import com.a3dweijing.wetouch2.network.RetrofitUtil;
import com.a3dweijing.wetouch2.View.GlideRoundedCornersTransform;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.ConnetionDate;
import com.a3dweijing.wetouch2.bean.GoodsInfoBean;
import com.a3dweijing.wetouch2.constant.Api;
import com.a3dweijing.wetouch2.datasave.Singleton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class MyPriviteActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.top_text)
    TextView topText;

    @BindView(R.id.top_all)
    RelativeLayout topAll;
    @BindView(R.id.top_right)
    TextView topRight;

    MyPrivateGridAdapter adapter;
    List<GoodsInfoBean> mdate = new ArrayList<>();

    @BindView(R.id.grid_my)
    GridView gridMy;
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.loading_date)
    LinearLayout loadingDate;

    boolean isPause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_privite);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
            getData();
    }

    private void initView() {
        topText.setText("我的收藏");
        //在初始化界面加载adapter适配器
        if (adapter == null) {
            adapter = new MyPrivateGridAdapter();
            gridMy.setAdapter(adapter);
        }
        gridMy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //点击进入商品详情，需要传入ID和啥？
                int id = mdate.get(i).getS_goods_id();
                ShoppingDetailsActivity.startActivity(MyPriviteActivity.this, id);
            }
        });
    }

    public void getData() {
        loadingDate.setVisibility(View.VISIBLE);
        int id = Integer.parseInt(Singleton.getInstance().getUser().getUser_id());
        Api api = RetrofitUtil.getInstance().getApiOfPhp();
        Observable<ConnetionDate> cData = api.getPrivteList(id);
        cData.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ConnetionDate>() {
           //请求中的操作
            @Override
            public void onSubscribe(Disposable d) {
                disposables.add(d);
            }

            //请求完成后的操作，与error互斥
            @Override
            public void onComplete() {
            }
            //请求成功后接下来的操作
            @Override
            public void onNext(ConnetionDate connetionDate) {
                if (connetionDate.isFlag()) {
                    List<GoodsInfoBean> list = connetionDate.getCollectionList();
                    if (list != null &&list.size()> 0  ) {
                        resautStatus.setVisibility(View.GONE);
                        mdate.clear();
                        mdate.addAll(list);
                    } else {
                        resautStatus.setVisibility(View.VISIBLE);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    resautStatus.setVisibility(View.VISIBLE);
                }
                loadingDate.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable e) {
                resautStatus.setVisibility(View.VISIBLE);
            }
        });
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    static class ViewHolder
    {
        ImageView item_img;
        TextView item_name;
        TextView item_price;
    }

    class MyPrivateGridAdapter extends BaseAdapter {
        Context context = MyPriviteActivity.this;
        LayoutInflater inflater;
        public MyPrivateGridAdapter() {
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            if (mdate != null) {
                return mdate.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;

      if (view == null) {
          view = inflater.inflate(R.layout.my_private_item,null);
          viewHolder = new ViewHolder();
          viewHolder.item_img = view.findViewById(R.id.item_img);
          viewHolder.item_name = view.findViewById(R.id.item_name);
          viewHolder.item_price = view.findViewById(R.id.item_price);
          view.setTag(viewHolder);
      } else{
          viewHolder = (ViewHolder) view.getTag();
      }
            GlideUtil.getInstance().showByCircle(context, mdate.get(i).getS_goods_img_url(), viewHolder.item_img,11, GlideRoundedCornersTransform.CornerType.TOP);
            viewHolder.item_name.setText(mdate.get(i).getS_goods_name());
            viewHolder.item_price.setText("￥" + mdate.get(i).getS_price());

            return view;
        }
    }

}
